<template>
  <div class="container">
    <div class="header">
      <van-nav-bar title="共享工厂" left-text="返回" right-text left-arrow @click-left="onClickLeft"/>
    </div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <div class="serach_box">
        <div class="left">
          <img src="../assets/images/adress.png" alt>
          <span>{{positionText}}</span>
        </div>
        <div class="center">
          <van-search
            placeholder="搜索"
            v-model="searchValue"
            @search="searchMethod"
            @clear="clearSearch"
          />
        </div>
        <div class="right" @click="toMyAdopt">
          <img src="../assets/images/user.png" alt>
          <span>我的记录</span>
          <div class="mark"></div>
        </div>
      </div>
      <div class="banner">
        <img src="../assets/images/banner.png" alt>
      </div>
      <div class="tab">
        <van-tabs
          v-model="active"
          :line-height="lineHeight"
          swipeable
          animated
          ellipsis
          :swipe-threshold="swipeNum"
          @change="tabChange"
        >
          <van-tab :title="item.name" v-for="(item,index) in tabList" :key="index">
            <div class="list">
              <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <ul>
                  <li
                    v-for="(item,index) in initData.list"
                    :key="index"
                    @click="toFarmIndex(item)"
                  >
                    <div class="box_content">
                      <div class="left">
                        <img :src="item.logo_url" alt>
                      </div>
                      <div class="right">
                        <div class="title">{{item.shopName}}</div>
                        <div class="rate">
                          <van-rate
                            v-model="item.rating"
                            color="#01bf47"
                            :size="22"
                            readonly
                            void-color="#cccccc"
                          />
                        </div>
                        <div class="subtitle">{{item.description}}</div>
                        <div class="classification">
                          <div v-for="(at,eq) in item.keyvalueList" :key="eq">{{at}}</div>
                        </div>
                        <div class="adress">
                          <img src="../assets/images/adress3.png" alt>
                          <span>{{item.address||"暂无"}}</span>
                        </div>
                      </div>
                    </div>
                  </li>
                </ul>
              </van-list>
            </div>
          </van-tab>
        </van-tabs>
      </div>
    </van-pull-refresh>
    <positionMap ref="pMap"></positionMap>
    <b>


    </b>
  </div>
</template>

<script>
import positionMap from "../components/positionMap";
export default {
  name: "factorylist",
  components: {
    positionMap
  },
  data() {
    return {
      //定位省市区
      positionText: "",
      swipeNum: 6,
      lineHeight: 0,
      active: 0,
      tabList: [],
      searchValue: "",
      value: 4,
      initData: {},
      initDataList: [],
      typeId: "",
      //下拉刷新
      isLoading: false,
      //上拉加载
      loading: false,
      finished: false,
      pageNum: 1,
      positionAddress:{},
      titleName:''
    };
  },
  watch: {
    //获取定位地址
    positionAddress() {
      if (this.positionAddress&&this.positionAddress.addressComponent) {
        this.positionText =
          this.positionAddress.addressComponent.province
      }
    }
  },

  mounted() {
    if(this.$route.query.titleName){
      this.titleName=decodeURIComponent(this.$route.query.titleName);
    };
    // alert(this.$cookies.get('token'));
    this.getTabList();
    this.getInitData();
  },
  methods: {
    //返回APP
    onClickLeft() {
      // this.$toast("返回");
      window.webkit.messageHandlers.goBackApp.postMessage(null);
    },
    //搜索
    searchMethod() {
      this.getInitData();
    },
    //清除搜索
    clearSearch() {
      this.getInitData();
    },
    //跳转农场主页
    toFarmIndex(itemData) {
      let params={
        itemData:itemData
      };
      window.webkit.messageHandlers.goFarmIndex.postMessage(itemData);
      // this.$router.push({
      //   path: "./farmIndex",
      //   query: {
      //     shopId: shopId
      //   }
      // });
    },
    //tab切换
    tabChange(index, title) {
      if (this.tabList[index].id) {
        this.typeId = this.tabList[index].id;
      } else {
        this.typeId = "";
      }
      this.initDataList = [];
      this.pageNum = 1;
      this.finished = false;
      this.getInitData();
    },
    //下拉刷新
    onRefresh() {
      this.initDataList = [];
      this.pageNum = 1;
      this.finished = false;
      this.getTabList();
      this.getInitData();
    },
    //上拉加载
    onLoad() {
      if (this.initData.list && this.initData.list.length > 0) {
        this.getInitData();
      }
    },
    //获取tab列表
    async getTabList() {
      let params = {
          // parentId: 222,
          // isAll: 1
          typeName:'生态加工'
      };
      const res = await this.$axios.post(this.$api.typeLists, params);
      if (res.success) {
        this.tabList = res.data;
        this.isLoading = false;
        this.loading = false;
      } else {
        this.tabList = [];
        this.$toast(res.msg);
      }
    },
    //获取列表数据
    async getInitData() {
      let params = {
        pageNum: this.pageNum,
        pageSize: 10,
        framName: this.searchValue || "",
        type_Id: this.typeId || "",
        key_value:this.titleName||"共享工厂",
        // province_code:"",
        // city_code:"",
        // district_code:"",
        // province:"",
        // type:"update"
      };
      const res = await this.$axios.post(this.$api.sharingFarmList, params);
      if (res.success) {
        if (this.loading) {
          this.initDataList = this.initDataList.concat(res.data.list);
          this.initData = res.data;
          this.initData.list = this.initDataList;
          if (this.initData.list.length >= this.initData.total) {
            this.finished = true;
          } else {
            this.pageNum++;
          }
        } else {
          this.initData = res.data;
          if (this.initData.list.length >= this.initData.total) {
            this.finished = true;
          }
        }
        this.initData.list.forEach((item, index) => {
          item.keyvalueList = item.key_value.split(",");
        });

        this.isLoading = false;
        this.loading = false;
      } else {
        this.initData = {};
        this.$toast(res.msg);
      }
    },
    toMyAdopt(){
      this.$router.push({
        path:'/myAdopt',query:{

        }
      });
    }
  }
};
</script>

<style scoped>
.nav {
  width: 100%;
  height: 0.4rem;
  background: #fff;
  display: flex;
  display: -webkit-flex;
  justify-content: space-around;
  align-items: center;
}
.nav .item {
  display: flex;
  display: -webkit-flex;
  align-items: center;
  font-size: 0.15rem;
  color: #333333;
}
.nav .mark {
  color: #01bf47;
}
.nav .item img {
  width: 0.1rem;
}
.nav .item span {
  padding-right: 0.06rem;
}
.container {
  padding-top: 0.46rem;
  width: 100%;
}
.header {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  z-index:100;
}
.serach_box {
  width: 100%;
  height: 0.3rem;
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.22rem;
  padding: 0 0.16rem;
}
.serach_box .left {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
  width: 20%;
}
.serach_box .left img {
  width: 0.13rem;
}
.serach_box .left span {
  font-size: 0.14rem;
  color: #000000;
  padding-left: 0.06rem;
}
.serach_box .right {
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  width: 25%;
  height: 100%;
  background: #fff;
  box-shadow: 0px 3px 8px 0px rgba(107, 107, 107, 0.19);
  border-radius: 5px;
  -webkit-border-radius: 5px;
  position: relative;
}
.serach_box .right img {
  width: 0.12rem;
}
.serach_box .right span {
  font-size: 0.12rem;
  color: #00bf46;
  padding-left: 0.04rem;
}
.serach_box .right .mark {
  width: 0.09rem;
  height: 0.09rem;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  background: #f44736;
  position: absolute;
  top: -0.04rem;
  right: -0.04rem;
}
.serach_box .center {
  width: 55%;
  height: 100%;
}
.van-search {
  padding: 0 0.14rem;
}
.banner {
  width: 100%;
  margin-top: 0.15rem;
  padding: 0 0.08rem;
}
.banner img {
  width: 100%;
}
.list {
  width: 100%;
  height: 100%;
  padding: 0 0.16rem;
}
.list li {
  width: 100%;
  background: #fff;
  border-radius: 0.1rem;
  -webkit-border-radius: 0.1rem;
  margin-top: 0.08rem;
  padding: 0 0.15rem;
}
.list .box_content {
  width: 100%;
  /* height: 1.56rem; */
  padding: 0.15rem 0;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
  border-bottom: 1px solid #f7f7f7;
}
.list .box_content .left {
  min-height: 1.05rem;
  display: flex;
  -webkit-display: flex;
  justify-content: center;
  align-items: center;
}
.list .box_content .left img {
  width: 1.05rem;
  border-radius: 0.05rem;
  -webkit-border-radius: 0.05rem;
}
.list .box_content .right {
  padding-left: 0.15rem;
}
.list .box_content .right .title {
  font-size: 0.15rem;
  color: #333333;
  margin-bottom: 0.06rem;
}
.list .box_content .right .subtitle {
  font-size: 0.12rem;
  color: #8c8c8c;
  margin-bottom: 0.06rem;
}
.list .box_content .right .classification {
  display: flex;
  -webkit-display: flex;
  flex-wrap: wrap;
}
.list .box_content .right .classification > div {
  /* width: 0.45rem; */
  height: 0.2rem;
  background: #e4ffeb;
  color: #04b746;
  border-radius: 3px;
  display: flex;
  -webkit-display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 0.1rem;
  line-height: 0;
  padding:0 .08rem;
   margin-bottom: 0.06rem;
}
.list .box_content .right .adress {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-start;
  align-items: center;
 
}
.list .box_content .right .adress img {
  width: 0.13rem;
}
.list .box_content .right .adress span {
  font-size: 0.14rem;
  color: #808080;
  padding-left: 0.06rem;
}
.box_footer {
  width: 100%;
  height: 0.42rem;
  display: flex;
  -webkit-display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.12rem;
  color: #333333;
}
.box_footer span {
  color: #f44736;
  font-size: 0.16rem;
}
</style>


<style>
.van-tab--active {
  color: #01bf47;
}
.van-search .van-cell {
  border-radius: 50px;
  background: #e8e8e8;
}
</style>
